﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace KinConquer.Packets.Handles
{
    public class GameConnect
    {
        public unsafe static void Handle(Byte* Data, KinSocket.Connection Client)
        {
            Structures.GameConnect* Packet = (Structures.GameConnect*)Data;

            Boolean authentic = false;
            if (!Client.Var.HasFlag(KinSocket.Connection.VarTypes.BypassGameAuthCheck))
            {
                MySqlDataReader reader = null;
                if (Program.Database.ExecuteReader("SELECT * FROM authenticate WHERE identifier = " + Packet->Identifier, ref reader) == 0)
                {
                    if (reader != null)
                    {
                        if (reader.Read())
                        {
                            TimeSpan tp;
                            if (TimeSpan.TryParse(reader["time"].ToString(), out tp))
                            {
                                if (DateTime.Now.TimeOfDay.TotalMilliseconds - tp.TotalMilliseconds < 10000)
                                    authentic = true;
                            }
                        }
                        reader.Close();
                    }
                }
            }
            else
            {
                Client.Var &= ~KinSocket.Connection.VarTypes.BypassGameAuthCheck;
                authentic = true;
            }
            Program.Database.ExecuteNonQuery("DELETE FROM authenticate WHERE identifier = " + Packet->Identifier);

            if (authentic)
            {
                Client.Identifier = Packet->Identifier;

                Byte[] response = new Structures.MessagePacket("Unable to load character. Contact an admin if this problem persists.", Structures.MessagePacket.Types.LoginInformation);
                Boolean remove = true;
                MySqlDataReader accData = null;
                if (Program.Database.ExecuteReader("SELECT type FROM account WHERE id = " + Client.Identifier, ref accData) == 0)
                {
                    if (accData != null)
                    {
                        if (accData.Read())
                        {
                            Client.Type = (KinSocket.Connection.Types)Convert.ToByte(accData["type"]);
                            if (Client.Type == KinSocket.Connection.Types.Banned)
                            {
                                response = Structures.MessageConst.BannedAccount;
                            }
                            else if (Client.Type == KinSocket.Connection.Types.BannedPERM)
                            {
                                response = Structures.MessageConst.BannedAccountPERM;
                            }
                            else
                            {
                                remove = false;

                                Entities.Hero Hero = new Entities.Hero(Client);
                                if (Hero.Load())
                                {
                                    Entities.Calculations.HitPoints.Calculate(ref Hero);

                                    
                                    Program.HeroPool.Add(Hero.Identifier, Hero);
                                    response = Structures.MessageConst.ANSWER_OK;

                                    Client.Send((Byte[])new Structures.HeroDataPacket(Hero));
                                    Client.Send((Byte[])new Structures.ConquerTime());
                                }
                                else
                                {
                                    response = Structures.MessageConst.NEW_ROLE;
                                }
                            }
                        }
                        accData.Close();
                    }
                }
                if (remove) Program.GameConnections.Remove(Client.Identifier);
                Client.Send(response);
            }
            else
            {
                Program.GameConnections.Remove(Client.Identifier);
                Client.Disconnect("Unauthenticated Game Login");
            }
        }
    }
}
